// pages/detail-search/index.js
import { getHotSearch , getSearchSuggest} from "../../service/api_search"

Page({

  data: {
    hotKeyword:[],
    suggestSongs:[],
    searchValue: ""
  },

  onLoad:function(options){
    // 获取页面数据
    this.getPageData()
  },
// 网络请求
  getPageData:function(){
    getHotSearch().then(res=>{
      this.setData({hotKeyword:res.result.hots})
    })
  },

  // 事件处理
  handleSearchChange:function(event){
    //1 获取输入关键字
    const searchValue = event.detail
    // 2保存输入关键字
    this.setData({searchValue})
    // 3判断关键字为空字符串的处理逻辑
    if(!searchValue.length){
      this.setData({suggestSongs:[]})
      return
    } 
    // 根据关键字进行搜索
    getSearchSuggest(searchValue).then(res=>{
      this.setData({suggestSongs: res.result.allMatch})
    })
  }
 
})
